package com.example.springboot.entity;

import jakarta.persistence.*;
import lombok.Data;
import com.baomidou.mybatisplus.annotation.*;

import java.sql.Date;
import java.util.List;

@Data
@Entity
@Table(name = "story")
@TableName("story")
public class Story {
    @Id
    @TableId(type = IdType.AUTO)
    private Long id;

    private String title;

    @Column(columnDefinition = "TEXT")
    private String excerpt;

    @Column(columnDefinition = "TEXT")
    private String content;

    private String category;

    @Column(name = "cover_image")
    private String coverImage;

    @Column(name = "author_id")
    private Long authorId;
    
    @Column(name = "destination_id")
    private Long destinationId;

    private Integer status;
    private Integer views;
    private Integer likes;

    @Column(name = "publish_date")
    private Date publishDate;

    @Column(name = "update_date")
    private Date updateDate;

    // 优化关联关系
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "destination_id", insertable = false, updatable = false)
    @TableField(exist = false)
    private Destination destination;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "author_id", insertable = false, updatable = false)
    @TableField(exist = false)
    private Author author;

    // 修改映射关系，使用正确的关联字段
    @OneToMany
    @JoinColumn(name = "story_id")
    @TableField(exist = false)
    private List<StoryTag> tags;

    @OneToMany
    @JoinColumn(name = "story_id")
    @TableField(exist = false)
    private List<StoryComment> comments;

    // 使用中间表处理多对多关系
    @ManyToMany
    @JoinTable(
        name = "related_story",
        joinColumns = @JoinColumn(name = "story_id"),
        inverseJoinColumns = @JoinColumn(name = "related_story_id")
    )
    @TableField(exist = false)
    private List<Story> relatedStories;
}